package cn.itlaobing.entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/20 0020.
 */
public class ShopCart implements Serializable {

    /**
     * key 商品id
     * goods 商品对象
     */
    private Map<Integer,Goods> data=new HashMap<>();

    public void add(Goods goods){
        if(data.containsKey(goods.getId())){
            Goods g=data.get(goods.getId());
            g.setCount(g.getCount()+1);
        }else{
            goods.setCount(1);
            data.put(goods.getId(),goods);
        }
    }
    public void remove(Integer goodsId){
        data.remove(goodsId);
    }

    /**
     * 修改数量
     * @param goodsId
     * @param count
     */
    public void updateCount(Integer goodsId,Integer count){
        if(data.containsKey(goodsId)){
            if(count>0){
                data.get(goodsId).setCount(count);
            }
        }
    }

    /**
     * 购物车中所有的商品
     * @return
     */
    public Collection<Goods> getGoods(){
        return data.values();
    }

    /**
     * 计算总价格
     * @return
     */
    public Float getTotalPrice(){
        float total=0F;
        for (Goods goods :getGoods()){
            total+=goods.getPrice()*goods.getCount();
        }
        return total;
    }

}
